
# -*- coding: utf-8 -*-
#
# Collector, the Audio CD collection manager
# Copyright (C) 2006 Piotr Gaczkowski
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import xml.dom, xml.dom.minidom
from xml.dom.minidom import parse

class Deserializer:
  def __init__(self, filename):
    self.filename = filename

  def write(self):
    dom = parse(self.filename)
    data = []
    for album_node in dom.getElementsByTagName("album"):
      print "albumid: "+album_node.getAttribute("id")
      for album_cn in album_node.childNodes:
        if album_cn.nodeType == album_cn.ELEMENT_NODE:
          print "- "+album_cn.nodeName
          if album_cn.nodeName == 'discid':
            print "-- "+album_cn.childNodes[0].data.strip()
          elif album_cn.nodeName == 'artist':
            print "--- "+album_cn.getAttribute("artistid")
            for artist_cn in album_cn.childNodes:
              if artist_cn.nodeType == artist_cn.ELEMENT_NODE:
                if artist_cn.nodeName == 'name':
                  print "---- "+artist_cn.childNodes[0].data.strip()
                elif artist_cn.nodeName == 'sortname':
                  print "---- "+artist_cn.childNodes[0].data.strip()
          elif album_cn.nodeName == 'asin':
            print "-- "+album_cn.childNodes[0].data.strip()
          elif album_cn.nodeName == 'title':
            print "-- "+album_cn.childNodes[0].data.strip()
          elif album_cn.nodeName == 'releaseEvents':
            for releaseEvents_cn in album_cn.childNodes:
              if releaseEvents_cn.nodeType == releaseEvents_cn.ELEMENT_NODE:
                if releaseEvents_cn.nodeName == 'date':
                  print "---- "+releaseEvents_cn.getAttribute("country")
                  print "---- "+releaseEvents_cn.childNodes[0].data.strip()
          elif album_cn.nodeName == 'tracks':
            for tracks_cn in album_cn.childNodes:
              if tracks_cn.nodeType == tracks_cn.ELEMENT_NODE:
                if tracks_cn.nodeName == 'track':
                  print "---- "+tracks_cn.getAttribute("duration")
                  for track_cn in tracks_cn.childNodes:
                    if track_cn.nodeType == track_cn.ELEMENT_NODE:
                      if track_cn.nodeName == 'title':
                        print "----- "+track_cn.childNodes[0].data.strip()
          elif album_cn.nodeName == 'types':
            for types_cn in album_cn.childNodes:
              if types_cn.nodeType == types_cn.ELEMENT_NODE:
                if types_cn.nodeName == 'type':
                  print "-- "+types_cn.getAttribute("ns")

#          elif album_cn.nodeName == '':
#            pass


  def _getText(nodelist):
    rc = ""
    for node in nodelist:
      if node.nodeType == node.TEXT_NODE:
        rc = rc + node.data
    return rc

